﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.OleDb;

public partial class ChapterEdit : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            GetChapterListInfo();
            lbStatus.Text = "";
        }
    }
    protected void GetChapterListInfo()
    {
        DBOperate myDB = new DBOperate();
        myDB.SetCommandString("Select * from ChapterList");
        gvChapterInfo.DataSource = myDB.ExecuteReader();
        gvChapterInfo.DataBind();
        myDB.CloseDB();
    }
    protected void GridView1_RowCancelingEdit(object sender, GridViewCancelEditEventArgs e)
    {
        gvChapterInfo.EditIndex = -1;
        GetChapterListInfo();
    }
    protected void gvChapterInfo_RowDeleting(object sender, GridViewDeleteEventArgs e)
    {
        //获取编号
        string no = gvChapterInfo.DataKeys[e.RowIndex].Value.ToString();
        try
        {
            string cmd = string.Format("delete from ChapterList where [No] = {0}", no);
            DBOperate myDB = new DBOperate();
            myDB.SetCommandString(cmd);
            myDB.ExecuteNonReader();
            myDB.CloseDB();
        }
        catch
        {
            return;
        }
        //如果执行到这一步说明删除成功，可以从新绑定一次数据表.
        Response.Write("<script>alert(\"删除成功！\")</script>");
        GetChapterListInfo();
    }
    protected void gvChapterInfo_RowUpdating(object sender, GridViewUpdateEventArgs e)
    {
        string no = gvChapterInfo.DataKeys[e.RowIndex].Value.ToString();
        string chapterNo = ((TextBox)gvChapterInfo.Rows[e.RowIndex].FindControl("tbCNo")).Text.Trim();
        string chapterName = ((TextBox)gvChapterInfo.Rows[e.RowIndex].FindControl("tbCName")).Text.Trim();
        if (chapterNo != "")
        {
            try
            {
                int.Parse(chapterNo);
            }
            catch
            {
                Response.Write("<script>alert(\"章节编号不能为非数字!\")</script>");
                gvChapterInfo.EditIndex = -1;
                GetChapterListInfo();
                return;
            }
        }
        //开始更新到数据库
        try
        {
            string cmd = string.Format(@"Update ChapterList 
                                    set CNo = {0},CName = '{1}' where [No] = {2}",
                                     chapterNo,
                                     chapterName,
                                     no);
            DBOperate myDB = new DBOperate();
            myDB.SetCommandString(cmd);
            myDB.ExecuteNonReader();
            myDB.CloseDB();
            Response.Write("<script>alert(\"修改成功！\")</script>");
        }
        catch(Exception ex)
        {
            Response.Write(ex.Message);
        }
        gvChapterInfo.EditIndex = -1;
        GetChapterListInfo();
    }
    protected void gvChapterInfo_RowEditing(object sender, GridViewEditEventArgs e)
    {
        gvChapterInfo.EditIndex = e.NewEditIndex;
        GetChapterListInfo();
    }
    //点击提交后执行插入数据库
    protected void btnSubmit_Click(object sender, EventArgs e)
    {
        string cNo = tbChapterNo.Text;
        string cName = tbChapterName.Text;
        if (cNo != "")
        {
            try
            {
                int.Parse(cNo);
            }
            catch
            {
                lbStatus.Text = "章节编号必须为数字或为空。";
                return;
            }
        }
        if (cName == "")
        {
            lbStatus.Text = "章节名称不能为空";
            return;
        }
        try
        {
            DBOperate myDB = new DBOperate();
            myDB.SetCommandString(string.Format("Select * from ChapterList where CName = '{0}'", cName));
            OleDbDataReader dbReader = myDB.ExecuteReader();
            if(!dbReader.Read())
            {
                myDB.CloseDB();
                myDB = new DBOperate();
                myDB.SetCommandString(string.Format(@"Insert into ChapterList(CNo,CName) values({0},'{1}')", cNo == "" ? "null" : cNo, cName));
                myDB.ExecuteNonReader();
                lbStatus.Text = "新增章节成功";
            }
            else
            {
                lbStatus.Text = "失败:数据库中已存在相同的章节和编号";
            }
            myDB.CloseDB();
        }
        catch(Exception ex)
        {
            lbStatus.Text = "失败:" + ex.Message;
        }
        GetChapterListInfo();
    }
}
